package com.hilaver.dzmis.dao;

import java.util.List;

import org.hibernate.Query;

import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.product.ProductDZColor;

public class ProductIdentificationDAO extends BaseHibernateDAO {
	
	public List getMaxReferenceIndex(String reference) throws Exception{
		String hql = "select max(referenceIndex) from " + ProductIdentification.class.getName() + " where reference = ? and ctDesignType = ?";
		try {
			Query queryObject = getSession().createQuery(hql);
			queryObject.setParameter(0, reference);
			queryObject.setParameter(1, "4");//大生产版才有编号款号内部编号
			return queryObject.list();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public List getMaxReferenceIndex(String ctCollarType, String ctSeasonNumber) throws Exception{
		String hql = "select max(referenceIndex) from " + ProductIdentification.class.getName() + " where ctCollarType = ? and ctSeasonNumber = ?";
		try {
			Query queryObject = getSession().createQuery(hql);
			queryObject.setParameter(0, ctCollarType);
			queryObject.setParameter(1, ctSeasonNumber);
			return queryObject.list();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public List getDifferentVariant(Integer id) throws Exception{
		String hql = "select distinct(variant) from " + ProductDZColor.class.getName() + " where productIdentification.id = ?";
		try{
			Query queryObject = getSession().createQuery(hql);
			queryObject.setParameter(0, id);
			return queryObject.list();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public List getAllIsNotHistoryPagination(String className, int offset, int limit,
			String sort, String order, String[] filters) throws Exception {
		try {
			String whereStatement = getWhereStatement(filters);
			if("".equals(whereStatement)){
				whereStatement= " where ";
			}else{
				whereStatement+= " and ";
			}
			String queryString = "from " + className + whereStatement + "(isConfirmed='02' or (isConfirmed='01' and ctDesignType<>4)) order by " + sort
					+ " " + order;
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setFirstResult(offset);
			queryObject.setMaxResults(limit);
			return queryObject.list();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public List getAllInProcessPagination(int offset, int limit,
			String sort, String order, String[] filters) throws Exception{
		try {
			String whereStatement = getWhereStatement(filters);
			if("".equals(whereStatement)){
				whereStatement = " where ";
			}else{
				whereStatement += " and ";
			}
			String queryString = "from " + ProductIdentification.class.getName() + whereStatement
				+ "(((isInprocess1='01' or isInprocess1 is null) and orderProducts.size>0) or ((isInprocess2='01' or isInprocess2 is null) and productLaunchings.size>0)) order by " 
				+ sort + " " + order;
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setFirstResult(offset);
			queryObject.setMaxResults(limit);
			return queryObject.list();
		}catch (Exception e){
			e.printStackTrace();
			throw e;
		}
	}
	
	public int getAllInProcessCount(String[] filters) throws Exception{
		try {
			String whereStatement = getWhereStatement(filters);
			if("".equals(whereStatement)){
				whereStatement = " where ";
			}else{
				whereStatement += " and ";
			}
			String queryString = "select count(*) from " + ProductIdentification.class.getName() + whereStatement
			+ "(((isInprocess1='01' or isInprocess1 is null) and orderProducts.size>0) or ((isInprocess2='01' or isInprocess2 is null) and productLaunchings.size>0))";
			Query queryObject = getSession().createQuery(queryString);
			return ((Integer) queryObject.iterate().next()).intValue();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public int getCountIsNotHistory(String className, String[] filters) throws Exception{
		try {
			String whereStatement = getWhereStatement(filters);
			if("".equals(whereStatement)){
				whereStatement= " where ";
			}else{
				whereStatement+= " and ";
			}
			String queryString = "select count(*) from " + className + whereStatement + "(isConfirmed='02' or (isConfirmed='01' and ctDesignType<>4))";
			Query queryObject = getSession().createQuery(queryString);
			return ((Integer) queryObject.iterate().next()).intValue();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
	
	public List getAllSaveAsPagination(String sort, String order, String[] filters) throws Exception {
		try {
			String whereStatement = getWhereStatement(filters);
			/*
			if("".equals(whereStatement)){
				whereStatement= " where ";
			}
			else{
				whereStatement+= " and ";
			}
			String queryString = "from " + ProductIdentification.class.getName() + whereStatement + "(isConfirmed='01' or (isConfirmed='02' and ctDesignType=4)) order by " + sort
					+ " " + order;
					*/
			String queryString = "from " + ProductIdentification.class.getName() + whereStatement + " order by " + sort
			+ " " + order;
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
	}
}
